﻿using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace MefWallpaperAddin
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class GeoWallpaperUserControl : UserControl
	{
        public GeoWallpaperUserControl()
		{
			InitializeComponent();
            btnCompleteAuth.Visibility = Visibility.Collapsed;
		}

		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
            tbTitle.Text = "";
        }

		private bool SetError(Control control, string msg)
		{
			MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
			control.Focus();
			return false;
		}

        object FindNearbyImages_Lock = new object();

        private void btnUpdateTags_Click(object sender, RoutedEventArgs e)
        {
            btnUpdateTags.IsEnabled = false;

            Dispatcher.Invoke((Action)(() =>
            {
                ((GeoWallpaperAddinImpl)this.DataContext).FindNearbyImages();
                ((GeoWallpaperAddinImpl)this.DataContext).UpdateWallpaper();
                btnUpdateTags.IsEnabled = true;
            }));
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            btnRefresh.IsEnabled = false;

            Dispatcher.Invoke((Action)(() =>
            {
                ((GeoWallpaperAddinImpl)this.DataContext).UpdateWallpaper();
                btnRefresh.IsEnabled = true;
            }));
        }

        private void btnUpdateInterval_Click(object sender, RoutedEventArgs e)
        {
            ((GeoWallpaperAddinImpl)this.DataContext).UpdateInterval();
        }

        private void btnBeginAuth_Click(object sender, RoutedEventArgs e)
        {
            ((GeoWallpaperAddinImpl)this.DataContext).BeginUserAuth();
            btnCompleteAuth.Visibility = Visibility.Visible;
        }

        private void btnCompleteAuth_Click(object sender, RoutedEventArgs e)
        {
            ((GeoWallpaperAddinImpl)this.DataContext).CompleteUserAuth();
            btnCompleteAuth.Visibility = Visibility.Collapsed;
        }

        private void cbAutoUpdate_Click(object sender, RoutedEventArgs e)
        {
            ((GeoWallpaperAddinImpl)this.DataContext).UpdateInterval();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(((GeoWallpaperAddinImpl)this.DataContext).LastPhoto.WebUrl);
        }
	}

}
